/**
 * add by yang.bo
 * 应用管理
 */
define(['module/component/views/FormView', 'module/component/views/GridView',
		'module/hostapp/action/HostAppAction',
		'module/hostapp/tools/tools',
		"text!module/hostapp/templates/AppView.html"],
        function (FormView, GridView, hostAppAction, tools, templates) {
	var mainView = ngc.View.extend({
		template: ngc.compile(templates),
		events: {
			'click #queryBtn' : 'queryBtnClick',
			'click #cancelBtn' : 'cancelBtnClick'
	    },
	    _form: function(){
		      return this.getView("#form_content");
		  },
		_grid: function(){
			  return this.getView("#grid_content");
	  	},
	    queryBtnClick: function(){
	    	if(!this._form().valid()){	
				ngc.info("必填信息不完整！");
				return;
			}
	    	var queryData = this._form().getValues();
	    	hostAppAction.queryApp(queryData, function(data){
	    		//省份+运营商+位置+项目类型+环境类型
	    		for(var i=0; i<data.length; i++){
	    			var province = this.getOptionsName("provinceData", data[i].province);
	    			var unitUsage = this.getOptionsName("unitUsageData", data[i].unitUsage);
	    			var localhost = this.getOptionsName("localhost", data[i].localhost);
	    			var projectType = this.getOptionsName("projectType", data[i].projectType);
	    			var environmentType = this.getOptionsName("environmentType", data[i].environmentType);
	    			data[i].projectNameBak = province+unitUsage+localhost+projectType+environmentType+data[i].projectName;
	    		}
	    		console.log(data)
	    		this._grid().reloadData(data);
	    	}.bind(this));
	    },
	    
	    getOptionsName: function(fieldName, key){
	    	var obj = this.optionsSelect[fieldName];
	    	for(var i=0; i<obj.length; i++){
	    		if(obj[i].VALUE == key && obj[i].NAME != "无"){
	    			return obj[i].NAME; 
	    		}
	    	}
	    	return "";
	    },
	    
	    cancelBtnClick: function(){
	    	this._form().reset();
	    	this.queryBtnClick();
	    },
	    
		render: function () {
	         this.$el.append(this.template({}));
	         this.initForm();
	    },
	     
	    initialize: function () {
	    	 this.render();	    	
	    },
	    
	    initWidget:function(){
	    	 $("#tabs").tabs({
	                canClose:true,
	              paging: true,
	              autoResizable:true
	            });
	    },

	    afterRender: function () {
	    	this.initWidget();
	    },
	    
	    //主机管理form表单
	    initForm: function () {	
	    	var that = this;
			var config_elements = [
					{
						id: 'province',
						label: '省份',
						type : 'select',
						editable:true,
						enumCode : "province",
					}, {
						id: 'unitUsage',
						label: '运营商',
						type : 'select',
						enumCode : "unitUsage",
					}, {
						id: 'product',
						label: '所属产品',
						type : 'select',
						enumCode : "product",
					}, {
						id: 'localhost',
						label: '位置',
						type : 'select',
						enumCode : "localhost",
					}, {
						id: 'projectType',
						label: '项目类型',
						type : 'select',
						enumCode : "projectType",
					}, {
						id: 'environmentType',
						label: '环境类型',
						type : 'select',
						enumCode : "environmentType",
					}, {
						id: 'projectName',
						label: '项目名称',
						type : 'text',
					}];
			this.setView("#form_content", new FormView({
				config : {
					elements : config_elements
				}
			}));
			
			this.getView("#form_content").on("viewRenderAfter", function() {
				$.when(this._getDropItemDynValue("province"),
						this._getDropItemDynValue("unitUsage"),
						this._getDropItemDynValue("product"),
						this._getDropItemDynValue("localhost"),
						this._getDropItemDynValue("projectType"),
						this._getDropItemDynValue("environmentType")).then(function(data1, data2, data3, data4, data5, data6){
					that.optionsSelect = {
				    		provinceData : data1[0].data,
							unitUsageData : data2[0].data,
							productData : data3[0].data,
							localhost : data4[0].data,
							projectType : data5[0].data,
							environmentType : data6[0].data
					    }
				    that.options = {
			    		provinceData : tools.getGridFormatSelectTV(data1[0].data, "VALUE", "NAME"),
						unitUsageData : tools.getGridFormatSelectTV(data2[0].data, "VALUE", "NAME"),
						productData : tools.getGridFormatSelectTV(data3[0].data, "VALUE", "NAME"),
						localhost : tools.getGridFormatSelectTV(data4[0].data, "VALUE", "NAME"),
						projectType : tools.getGridFormatSelectTV(data5[0].data, "VALUE", "NAME"),
						environmentType : tools.getGridFormatSelectTV(data6[0].data, "VALUE", "NAME")
				    }
				    that.initGrid();
				}.bind(this));
				this.on("inputChange", function(cfg, e) {
					if((cfg == "province" && this.getValues()["province"] != '' ) ||
							(cfg == "unitUsage" && this.getValues()["unitUsage"] != '' ) ||
							(cfg == "product" && this.getValues()["product"] != '' )|| 
							(cfg == "localhost" && this.getValues()["localhost"] != '' )|| 
							(cfg == "projectType" && this.getValues()["projectType"] != '' )|| 
							(cfg == "environmentType" && this.getValues()["environmentType"] != '' )|| 
							(cfg == "projectName")){
						that.queryBtnClick();
					}
		    	});
			})
	    },
	    //应用管理grid
	    initGrid: function (){
	    	var that = this;
			var opt = {
		        height: 500,
		        colModel: [{
		            name: 'id',
		            label: 'ID',
		            key: true,
		            hidden: true
		        }, {
		        	name: 'province',
		        	label: '省份',
		        	formatter: "select",
		        	editoptions: { value: that.options.provinceData },
		        	width: 80
		        }, {
		        	name: 'unitUsage',
		        	label: '运营商',
		        	formatter: "select",
		        	editoptions: { value: that.options.unitUsageData },
		        	width: 80
		        }, {
		        	name: 'product',
		        	label: '所属产品',
		        	formatter: "select",
		        	editoptions: { value: that.options.productData },
		        	width: 100
		        }, {
		        	name: 'localhost',
		        	label: '位置',
		        	formatter: "select",
		        	editoptions: { value: that.options.localhost },
		        	width: 80
		        }, {
		        	name: 'projectType',
		        	label: '项目类型',
		        	formatter: "select",
		        	editoptions: { value: that.options.projectType },
		        	width: 80
		        }, {
		        	name: 'environmentType',
		        	label: '环境类型',
		        	formatter: "select",
		        	editoptions: { value: that.options.environmentType },
		        	width: 80
		        }, {
		        	name: 'projectNameBak',
		        	label: '项目',
		        	width: 200
		        }, {
		        	name: 'url',
		        	label: '应用URL',
		        	formatter: function(cellval, opts, rwdat, _act) {
		        		if(rwdat.url != null && rwdat.url != ""){
		        			return "<span style='color:#0f659c'>"+rwdat.url+"</span>";
		        		}
		        		return "";
		        	},
		        }, {
		        	name: 'responsibility',
		        	label: '负责人',
		        	width: 80
		        }],
		        toolbar: [true, "bottom"]
			};
			this.setView("#grid_content", new GridView({
				config : {					
					grid : opt,
					callback:{
						onCellSelect: function(e, rowid, iCol, cellcontent){
							if (iCol == 8){
								var _rowData = this.getRowData(rowid);
								if(_rowData.url != null && _rowData.url != ""){
									//在新窗口打开链接
									window.open(_rowData.url);
									//在本窗口打开链接
//								window.location.href = _rowData.url;
								}
							}
						}
					}
				}
			}));
			this.renderViews("#grid_content");
	        this.getView("#grid_content").on("viewRenderAfter", function() {
	        	if(global.edit){
	        		this.navButtonAdd([{
	        			title:"新增",
	        			id:"grid_content_addIcon",
	        			buttonicon:"glyphicon glyphicon-plus",
	        			navpos:"bottombar",
	        			caption:"新增",
	        			onClick:function(){
	        				that.gridAdd();
	        			}
	        		}
	        		]);
	        	}
	        	//处理是否有右键
	        	if(global.edit){
	        		this.attachMenu([{id:'add',label:'添加数据',icon:'fa fa-plus-square fa-lg',index:0},
	        		                 {id:'update',label:'修改数据',icon:'fa-pencil-square-o fa-lg',index:0},
	        		                 {id:'delete',label:'删除数据',icon:'fa fa-trash fa-lg',index:0},
	        		                 {id:'show',label:'查看数据',icon:'fa fa-search fa-lg',index:0}]);
	        	}else{
	        		this.attachMenu([{id:'show',label:'查看数据',icon:'fa fa-search fa-lg',index:0}]);
	        	}
				
				this.on("onClickMenu",function(menuId,selId,menu,e){
					switch (menuId) {
					case 'add':
						that.gridAdd();
						break;
					case 'update':
						that.gridUpdate(selId);
						break;
					case 'delete':
						that.gridDel(selId);
						break;
					case 'show':
						that.gridShow(selId);
						break;
					}
				});
				that.queryBtnClick();
			});
	    },
	    
	    gridAdd: function(){
	    	var that = this;
	    	ngc.openView({
	    		url: "module/hostapp/views/AppEditView",
	    		viewOption: {
	    			type : "add"
	    		},
	    		width: "80%",
	    		title: "应用添加"
	    	}).then(function(view){
	    		view.on('editView', function(data){
	    			that.queryBtnClick();
	    		});
	    	});
	    },
	    
	    gridUpdate: function(selId){
	    	var that = this;
	    	if(selId == null){
	    		ngc.info('请先选择一条数据进行修改操作!');
	    		return;
	    	}
	    	var _rowData = this._grid().getRowData(selId);
	    	ngc.openView({
	    		url: "module/hostapp/views/AppEditView",
	    		viewOption: {
	    			type : "edit",
	    			rowData : _rowData
	    		},
	    		width: "80%",
	    		title: "应用信息修改"
	    	}).then(function(view){
	    		view.on('editView', function(data){
	    			that.queryBtnClick();
	    		});
	    	});
	    },
	    
	    gridShow: function(selId){
	    	var that = this;
	    	if(selId == null){
	    		ngc.info('请先选择一条数据进行修改操作!');
	    		return;
	    	}
	    	var _rowData = this._grid().getRowData(selId);
	    	ngc.openView({
	    		url: "module/hostapp/views/AppEditView",
	    		viewOption: {
	    			type : "show",
	    			rowData : _rowData
	    		},
	    		width: "80%",
	    		title: "应用信息修改"
	    	}).then(function(view){
	    	});
	    },
	    
	    gridDel: function(rowid){
	    	var that = this;
	    	if(rowid == null){
	    		ngc.info('请先选择一条数据进行删除操作!');
	    		return;
	    	}
	    	ngc.confirm('确认是否删除此条数据?').result.then(function() {
	    		$.when(hostAppAction.delApp(rowid,$.noop))
		 		.then(function(data){
		 			if(data.resultStat == 'SUCCESS'){
		 				ngc.success("删除成功")
		 				that._grid().delRow(rowid);
		 			}
	    		});
			}.bind(this));
	    }
	});
	return mainView;
});
